resource "google_compute_vpn_tunnel" "tunnel1" {
  name          = "<%= ctx[:vars]['vpn_tunnel_name'] %>"
  peer_ip       = "15.0.0.120"
  shared_secret = "a secret message"

  target_vpn_gateway = google_compute_vpn_gateway.target_gateway.id

  depends_on = [
    google_compute_forwarding_rule.fr_esp,
    google_compute_forwarding_rule.fr_udp500,
    google_compute_forwarding_rule.fr_udp4500,
  ]
}

resource "google_compute_vpn_gateway" "target_gateway" {
  name    = "<%= ctx[:vars]['target_vpn_gateway_name'] %>"
  network = google_compute_network.network1.id
}

resource "google_compute_network" "network1" {
  name = "<%= ctx[:vars]['network_name'] %>"
}

resource "google_compute_address" "vpn_static_ip" {
  name = "<%= ctx[:vars]['address_name'] %>"
}

resource "google_compute_forwarding_rule" "fr_esp" {
  name        = "<%= ctx[:vars]['esp_forwarding_rule_name'] %>"
  ip_protocol = "ESP"
  ip_address  = google_compute_address.vpn_static_ip.address
  target      = google_compute_vpn_gateway.target_gateway.id
}

resource "google_compute_forwarding_rule" "fr_udp500" {
  name        = "<%= ctx[:vars]['udp500_forwarding_rule_name'] %>"
  ip_protocol = "UDP"
  port_range  = "500"
  ip_address  = google_compute_address.vpn_static_ip.address
  target      = google_compute_vpn_gateway.target_gateway.id
}

resource "google_compute_forwarding_rule" "fr_udp4500" {
  name        = "<%= ctx[:vars]['udp4500_forwarding_rule_name'] %>"
  ip_protocol = "UDP"
  port_range  = "4500"
  ip_address  = google_compute_address.vpn_static_ip.address
  target      = google_compute_vpn_gateway.target_gateway.id
}

resource "google_compute_route" "route1" {
  name       = "<%= ctx[:vars]['route_name'] %>"
  network    = google_compute_network.network1.name
  dest_range = "15.0.0.0/24"
  priority   = 1000

  next_hop_vpn_tunnel = google_compute_vpn_tunnel.tunnel1.id
}
